package com.booksystem.campuslibrarymanagementsystem.controller.user;


import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.Feedbackquest.DeleteFeedback;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.Feedbackquest.Feedback;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.Feedbackquest.Feedbacklist;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.FeedbacksTable;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.service.FeedbacksTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import com.booksystem.campuslibrarymanagementsystem.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/User")
@Api(tags = "信息反馈")
@RestController
public class MessageFeedbackController {

    /*
     信息反馈功能
     1，输入学号，反馈内容
     2，自动生成反馈时间
     3，状态默认为1(不通过)

     */
    @Autowired
    AccountTableService accountTableService;
    @Autowired
    FeedbacksTableService feedbacksTableService;

    public MessageFeedbackController(FeedbacksTableService feedbacksTableService) {
        this.feedbacksTableService = feedbacksTableService;
    }


    @PostMapping("/MessageFeedback")
    @ApiOperation(value = "信息反馈", notes = "通过学号，反馈内容进行用户反馈")
    public Result MessageFeedback(@RequestBody Feedback feedback) {
        Map<String, Object> parsed =JwtUtil.tokenPre(feedback.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {

                String content = feedback.getContent();//反馈内容
                String time = TimeUtil.getTime();

                    FeedbacksTable fee = new FeedbacksTable();
                    fee.setAccount((String) account); // 存储账户ID
                    fee.setContent(content);
                    fee.setTime(time);
                    fee.setState("未处理");
                    int a = feedbacksTableService.saveb(fee);
                    if (a > 0) {
                        return Result.success(fee, 1);
                    } else {
                        return Result.fail((String.format("该账户反馈失败：%s", account)));
                    }


            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }


    @PostMapping("/selectMessageFeedback")
    @ApiOperation(value = "查看信息反馈", notes = "通过学号，查看自己的全部反馈信息")
    public Result<List<FeedbacksTable>> selectMessageFeedback(@RequestBody Feedbacklist feedbacklist) {
        Map<String, Object> parsed = JwtUtil.tokenPre(feedbacklist.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                List<FeedbacksTable> title = feedbacksTableService.selectByaccount((String) account);//通过图书名称获得
                if (title.isEmpty()) {
                    return Result.fail("该账户id没有进行信息反馈");
                } else {
                    // 可能需要进一步处理书籍列表，比如分页或转换为API响应格式
                    // System.out.println(books);
                    return Result.success(title, 1);
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }

    }

    @PostMapping("/deletefeedback")
    @ApiOperation(value = "删除信息反馈",notes = "反馈id进行删除该账户下反馈信息记录")
    public Result deleteFeedbacklist(@RequestBody DeleteFeedback deleteFeedback) {
        Map<String, Object> parsed = JwtUtil.tokenPre(deleteFeedback.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                int  feedbackid = deleteFeedback.getId();  //反馈id
                int hh = feedbacksTableService.deleteFeedbackid((String)account,feedbackid);
                // 首先检查该用户是否已经收藏了这本书
                if (hh>0) {
                    return  Result.success("成功删除该反馈记录",1);
                }
                return Result.fail(String.format("没有该反馈记录记录：%s", feedbackid));
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }
}
